package com.example.dayu521.sg_oss.service.impl;

import com.example.dayu521.sg_oss.service.PreSignature;
import lombok.extern.slf4j.Slf4j;
import org.example.dayu521.exception.BadObjSize;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.function.Function;

@Slf4j
public class DressOss implements PreSignature {

    private Function<AskParam,AskResult> delegate=null;
    private BadObjSize e;

    @Value("${spring.application.name}")
    private String appname;

    @Override
    public void locale(LocalFileOss localFileOss) {
        delegate=askParam -> new AskResult("post","/"+appname+"/oss/upload","formdata");
        e=null;
    }

    @Override
    public void minio(MinioOss minioOss) {
        delegate=a->{
            var ppp=new MinioOss.AskParam(a.obj_name(),a.obj_size());
            MinioOss.AskResult res= null;
            try {
                res = minioOss.ask_obj_handle(ppp);
            } catch (BadObjSize ex) {
                e=ex;
                return null;
            }
            var qqq=new AskResult(res.method(), res.url(),"file");
            return qqq;
        };
        e=null;
    }

    @Override
    public AskResult ask_obj_handle(AskParam ask_param) throws BadObjSize {
        Objects.isNull(e);
        var res= delegate.apply(ask_param);
        if(e!=null){
            throw e;
        }
        return res;
    }
}
